package ms.unicorn.diagram.figures;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.PointList;
import org.eclipse.draw2d.geometry.PrecisionPoint;
import org.eclipse.gmf.runtime.draw2d.ui.geometry.PointListUtilities;
import org.eclipse.gmf.runtime.gef.ui.figures.SlidableAnchor;

public class SlidableAnchorEx extends SlidableAnchor {

	public SlidableAnchorEx() {

	}

	public SlidableAnchorEx(IFigure f) {
		super(f);

	}

	public SlidableAnchorEx(IFigure f, PrecisionPoint p) {
		super(f, p);

	}

	public Point getLocation(Point reference) {
			
		Point ownReference = getReferencePoint();
		
		Point location = getLocation(ownReference, reference);
		if (location == null) {
			location = getLocation(new PrecisionPoint(getBox().getCenter()), reference);
			if (location == null) {
				location = getBox().getCenter();
			}
		}
		
		return location;
	}
	
	protected Point getLocation(Point ownReference, Point foreignReference) {
		//System.out.println(ownReference.toString());
		///System.out.println(foreignReference.toString());
		//System.out.println("------------");
		PointList intersections = getIntersectionPoints(ownReference, foreignReference);
		if (intersections!=null && intersections.size()!=0) {
			Point location = PointListUtilities.pickClosestPoint(intersections,
					foreignReference);
			return location;
		}
		return null;
	}
	
}
